package net.jielan188.hd.bean.base;

import net.jielan188.hd.util.CommonUtils;
import org.apache.commons.lang.StringUtils;

/**
 * 查询条件封装
 * Created by hgg on 2017/1/12.
 */
public class MyBatisSelect {

    /**
     * 查询条件（自动封装所有的查询条件）
     */
    private String selectSql;

    /**
     * 普通的条件查询
     */
    private String selectCondition;

    /**
     * 不包含
     */
    private String selectNotIn;

    /**
     * 不等于
     */
    private String selectNotEqual;

    /**
     * 模糊匹配
     */
    private String selectLike;

    /**
     * 大于
     */
    private String selectGt;

    /**
     * 小于
     */
    private String selectLt;

    public String getSelectNotIn() {
        return selectNotIn;
    }

    public void setSelectNotIn(String selectNotIn) {
        this.selectNotIn = selectNotIn;
    }

    public String getSelectNotEqual() {
        return selectNotEqual;
    }

    public void setSelectNotEqual(String selectNotEqual) {
        this.selectNotEqual = selectNotEqual;
    }

    public String getSelectLike() {
        return selectLike;
    }

    public void setSelectLike(String selectLike) {
        this.selectLike = selectLike;
    }

    public String getSelectGt() {
        return selectGt;
    }

    public void setSelectGt(String selectGt) {
        this.selectGt = selectGt;
    }

    public String getSelectLt() {
        return selectLt;
    }

    public void setSelectLt(String selectLt) {
        this.selectLt = selectLt;
    }

    public String getSelectCondition() {
        return selectCondition;
    }

    public void setSelectCondition(String selectCondition) {
        this.selectCondition = selectCondition;
    }

    public String getSelectSql() {

        StringBuffer sql = new StringBuffer("where");
        if( StringUtils.isNotBlank( selectCondition ) ){
            getSelectSqlType( selectCondition.split( "," ),sql,1 );
        }

        if( StringUtils.isNotBlank( selectNotIn ) ){
            getSelectSqlType( selectNotIn.split( "&" ),sql,2 );
        }

        if( StringUtils.isNotBlank( selectNotEqual ) ){
            getSelectSqlType( selectNotEqual.split( "," ),sql,3 );
        }

        if( StringUtils.isNotBlank( selectLike ) ){
            getSelectSqlType( selectLike.split( "," ),sql,4 );
        }

        if( StringUtils.isNotBlank( selectGt ) ){
            getSelectSqlType( selectGt.split( "," ),sql,5 );
        }

        if( StringUtils.isNotBlank( selectLt ) ){
            getSelectSqlType( selectLt.split( "," ),sql,6 );
        }

        if( StringUtils.equals( sql.toString(),"where" ) ){
            return null;
        }
        return sql.toString();
    }

    public void setSelectSql(String selectSql) {
        this.selectSql = selectSql;
    }

    /**
     * 获取具体的SQL
     * @param fields
     * @param sql
     * @param type
     */
    private void getSelectSqlType(String[] fields, StringBuffer sql, Integer type ){

        if( fields != null && fields.length > 0 ){
            if( !StringUtils.equals( sql.toString(),"where" ) )
                sql.append( " and" );
            for (Integer i = 0; i < fields.length ; i ++ ){
                if( i != 0 )
                    sql.append( " and" );
                String[] thisField = fields[i].split( "_" );
                if( type == 1 ){
                    sql.append( " " ).append( CommonUtils.camel4underline( thisField[0] ) ).append( "=" ).append( "'" )
                            .append( thisField[1] ).append("'");
                }else if ( type == 2 ){
                    sql.append( " " ).append( CommonUtils.camel4underline( thisField[0] ) ).append( " not in ('" )
                            .append( thisField[1] ).append( "')" );
                }else if ( type == 3 ){
                    sql.append( " " ).append( CommonUtils.camel4underline( thisField[0] ) ).append( "<>" )
                            .append( "'" ).append( thisField[1] ).append( "'" );
                }else if ( type == 4 ){
                    sql.append( " " ).append( CommonUtils.camel4underline( thisField[0] ) ).append( " like" ).append( " '%" )
                            .append( thisField[1] ).append( "%'" );
                }else if ( type == 5 ){
                    sql.append( " " ).append( CommonUtils.camel4underline( thisField[0] ) ).append( " >= " )
                            .append( "'" ).append( thisField[1] ).append( "'" );
                }else if ( type == 6 ){
                    sql.append( " " ).append( CommonUtils.camel4underline( thisField[0] ) ).append( " <= " ) .append( "'" )
                            .append( thisField[1] ).append( "'" );
                }
            }
        }
    }

}
